﻿using HomeWork911.Api.Read.Appliaction.Commond;
using HomeWork911.Api.Read.Dto;
using HomeWork911.Domain;
using HomeWork911.ErrorCode;
using HomeWork911.infrastrunceture;
using MediatR;

namespace HomeWork911.Api.Read.Appliaction.CommondHandler
{
    public class GetPermissionCommondHandler : IRequestHandler<GetPermissionCommond, ApiResult<List<ShowPermissionDto>>>
    {
        private readonly IBaseIRepository<Permission> baseIRepository;

        public GetPermissionCommondHandler(IBaseIRepository<Permission> baseIRepository)
        {
            this.baseIRepository = baseIRepository;
        }

        public Task<ApiResult<List<ShowPermissionDto>>> Handle(GetPermissionCommond request, CancellationToken cancellationToken)
        {
            ApiResult<List<ShowPermissionDto>> result=new ApiResult<List<ShowPermissionDto>>();
            result.Code = ApiEnum.查询成功;
            result.Msg = ApiEnum.查询成功.ToString();
            var list = from a in baseIRepository.GetAll()
                       where a.PermissionId == request.PermissionId
                       select new ShowPermissionDto()
                       {
                           PermissionId = a.PermissionId,
                           PermissionName = a.PermissionName,
                           PermissionUrl = a.PermissionUrl,
                           Number = a.Number,
                       };
            result.data=list.ToList();
            return Task.FromResult(result);
        }
    }
}
